<template>
  <div class="form-conf">
    <v-form-render :form-json="formData" ref="vFormRef"></v-form-render>
    <div class="dialog-footer">
      <el-button type="info" icon="el-icon-circle-plus-outline" :loading="Addloading" @click="submitForm('0')">暂存</el-button>
      <el-button type="primary" icon="el-icon-circle-check" :loading="submitloading" @click="submitForm('1')">提交</el-button>
    </div>
  </div>
</template>

<script>
import { getProcessForm, startProcess,getProcessFormHold } from '@/api/workflow/process'
export default {
  name: "VformRenderDialog",
  props: {
    formData: {
      type: Object,
      required: true
      },
      definitionId: {
          type: String,
        required:true
      },
      code: {
          type: String,
        required:true
      },
      ioType:{
        type:Number,
      },
      budget: {
        type: Object,
      required: false
      }
  },
  data() {
    return {
      Addloading: false,
      submitloading:false
    }
  },
  computed: {
    _userInfo() {
        return this.$store.state.user.userInfo
      }
  },
  created() {
    // console.log(this.budget,'77777777');
  },
  mounted() {
    this.userInfo()
    // console.log(this.ioType,"进出类型，，，，，，，，，，，，，");

   },
  methods: {
    userInfo() {
      this.$refs.vFormRef.setFieldValue('nickName',this._userInfo.nickName)
      this.$refs.vFormRef.setFieldValue('deptName',this._userInfo.dept.deptName)
      this.$refs.vFormRef.setFieldValue('role', this._userInfo.roles[0].roleName)
      this.$refs.vFormRef.setFieldValue('newDate', this.$moment().format('YYYY-MM-DD'))
      if (this.$refs.vFormRef.getWidgetRef('phonenumber')) {
        this.$refs.vFormRef.setFieldValue('phonenumber', this._userInfo.phonenumber)
      }
      if (this.$refs.vFormRef.getWidgetRef('budgetProjectCode')) {
        this.$refs.vFormRef.setFieldValue('budgetProjectCode', this.budget.budgetProjectCode)
      }
      if (this.$refs.vFormRef.getWidgetRef('budgetProjectName')) {
        this.$refs.vFormRef.setFieldValue('budgetProjectName', this.budget.budgetProjectName)
      }
    },
    // 暂存表单
      submitAdd() {
        if (this.$refs.vFormRef.getWidgetRef('FJ')) {
        this.$refs.vFormRef.getWidgetRef('FJ').setRequired(false)
      }
      if (this.$refs.vFormRef.getWidgetRef('tranCode')) {
         this.$refs.vFormRef.getWidgetRef('tranCode').setRequired(false)
      }
      this.$refs.vFormRef.getFormData().then(obj => {
        this.Addloading = true
      //   .then(formData => {
      getProcessFormHold(this.definitionId, JSON.stringify(obj),this.code).then(res => {
        if (res.code == 200) {
          this.$modal.msgSuccess("暂存成功");
            this.Addloading = false
            this.$emit('vformDialog')
        //   this.$tab.closeOpenPage({
        //     path: "/work/own"
        //   });
        }
      });
      }).catch(error => {
            this.$message.error(error)
          })
      
      // });
    },
    // 提交表单按钮
      submitForm(type) {
        if (this.$refs.vFormRef.getWidgetRef('FJ')) {
        if (type == '0') {
        this.$refs.vFormRef.getWidgetRef('FJ').setRequired(false)
      } else {
        this.$refs.vFormRef.getWidgetRef('FJ').setRequired(true)
      }
      }
      if (this.$refs.vFormRef.getWidgetRef('tranCode')) {
        if (type == '0') {
        this.$refs.vFormRef.getWidgetRef('tranCode').setRequired(false)
      } else {
        this.$refs.vFormRef.getWidgetRef('tranCode').setRequired(true)
      }
      }
      // 校验表单
      this.$refs["vFormRef"].validateForm(valid => {
        // 获取切换框的值
        const val = this.$refs.vFormRef.getFieldValue("cut");
        // 判断是否有切换表单
        if (val.length !== 0) {
          if (val == 1) {
            if (valid) {
              // 当前展示表单校验通过
              this.validForm = true;
              // 赋值给切换框
              this.$refs.vFormRef.setFieldValue("cut", 2);
              // 再次校验当前表单
              this.$refs["vFormRef"].validateForm(validl => {
                if (validl) {
                  // 当前表单二校验通过
                  // console.log("1");
                  this.validForm2 = true;
                  this.cutForm(type);
                }
              });
            }
          } else if (val == 2) {
            // 如果表单二展示
            if (valid) {
              // 校验通过
              this.validForm2 = true;
              // 赋值给切换框
              // this.$nextTick(() => {
              this.$refs.vFormRef.setFieldValue("cut", 1);
              // })
              // 再次校验当前展示表单
              this.$refs["vFormRef"].validateForm(validlS => {
                // console.log(validlS, "kan");
                if (validlS) {
                  // 校验通过
                  this.validForm = true;
                  this.cutForm(type);
                }
              });
            }
          }
        } else {
            // console.log("不切换");
            if (type == '0') {
                this.submitAdd()
            
            } else {
            if (this.$refs.vFormRef.getWidgetRef('FJ')) {
            this.$refs.vFormRef.getWidgetRef('FJ').setRequired(true)
            }
           if (this.$refs.vFormRef.getWidgetRef('tranCode')) {
             this.$refs.vFormRef.getWidgetRef('tranCode').setRequired(true)
                }
            this.$refs.vFormRef.getFormData().then(formData => {
            this.submitloading = true
            startProcess(this.definitionId, JSON.stringify(formData),this.code).then(
              res => {
                this.$modal.msgSuccess('操作成功');
                this.submitloading = false
                this.$emit('vformDialog')
              }
            );
          }).catch(error => {
            this.$message.error(error)
          })
          }
          
        }
      });
    },

    // 表单校验方法
    cutForm(type) {
      // console.log(this.validForm, this.validForm2, "通过");
      if (this.validForm && this.validForm2) {
          this.$refs.vFormRef.setFieldValue("cut", 1);
          if (type == '1') {
              this.submitloading = true
            if (this.$refs.vFormRef.getWidgetRef('FJ')) {
            this.$refs.vFormRef.getWidgetRef('FJ').setRequired(true)
            }
           if (this.$refs.vFormRef.getWidgetRef('FJ')) {
             this.$refs.vFormRef.getWidgetRef('tranCode').setRequired(true)
            }
        this.$refs.vFormRef
          .getFormData()
          .then(formData => {
            startProcess(this.definitionId, JSON.stringify(formData),this.code).then(
              res => {
                this.$modal.msgSuccess('操作成功');
                this.submitloading = false
                this.$emit('vformDialog')
              }
            );
          })
          .catch(error => {
            // Form Validation failed
            this.$message.error(error);
          });
          } else {
            this.submitAdd()
        }
        
      } else {
      }
    }
  }
};
</script>

<style lang="scss" scoped>
.dialog-footer {
  margin: 15px auto;
  text-align: center;
}
</style>